package dao.impl.comm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.comm.IHomeworkDAO;
import vo.comm.Homework;

public class HomeworkDAOImpl implements IHomeworkDAO{
	private Connection conn=null;
	private PreparedStatement pstmt=null;
	public HomeworkDAOImpl(Connection conn){
		this.conn=conn;
	}
	public boolean doCreate(Homework homework) throws SQLException{
		boolean flag=false;
		String sql="INSERT INTO homework(homeworkname)VALUES(?)";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkname());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		this.pstmt.close();
		return flag;
	}
	public boolean doDelete(Homework homework) throws SQLException{
		boolean flag=false;
		String sql="DELETE FROM homework WHERE homeworkid=? OR homeworkname=?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkid());
		this.pstmt.setString(2, homework.getHomeworkname());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		if(this.pstmt!=null){
			this.pstmt.close();
		}
		return flag;
	}
	public Homework findByName(String homeworkname) throws SQLException{
		Homework homework=null;
		String sql="SELECT homeworkname FROM homework WHERE homeworkname=?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1,homeworkname);
		ResultSet rs=this.pstmt.executeQuery();
		if(rs.next()){
			homework=new Homework();
			homework.setHomeworkname(rs.getString(1));
		}
		this.pstmt.close();
		return homework;
	}
	public List<Homework> findAll(String keyWord) throws SQLException{
		List<Homework> all=new ArrayList<Homework>();
		String sql="SELECT homeworkid,homeworkname FROM homework WHERE homeworkname like ?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, "%"+keyWord+"%");
		ResultSet rs=this.pstmt.executeQuery();
		Homework homework=null;
		while(rs.next()){
			homework=new Homework();
			homework.setHomeworkid(rs.getString(1));
			homework.setHomeworkname(rs.getString(2));
			all.add(homework);
		}
		return all;
	}
	public boolean doUpdate(Homework homework) throws SQLException{
		boolean flag=false;
		String sql="UPDATE homework SET homeworkname=? WHERE homeworkid=?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkname());
		this.pstmt.setString(2, homework.getHomeworkid());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		if(this.pstmt!=null){
			this.pstmt.close();
		}
		return flag;
	}
	public boolean doPublish(Homework homework) throws SQLException{
		boolean flag=false;
		String sql="INSERT INTO publish_homework(homeworkid,homeworkname)VALUES(?,?)";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkid());
		this.pstmt.setString(2, homework.getHomeworkname());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		this.pstmt.close();
		return flag;
	}
	
	public List<Homework> findPublish(String keyWord) throws SQLException{
		List<Homework> all=new ArrayList<Homework>();
		String sql="SELECT homeworkid,homeworkname FROM publish_homework WHERE homeworkname like ?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, "%"+keyWord+"%");
		ResultSet rs=this.pstmt.executeQuery();
		Homework homework=null;
		while(rs.next()){
			homework=new Homework();
			homework.setHomeworkid(rs.getString(1));
			homework.setHomeworkname(rs.getString(2));
			all.add(homework);
		}
		return all;
	}
	
	public boolean doUpload(Homework homework) throws SQLException{
		boolean flag=false;
		String sql="INSERT INTO upload_homework(homeworkid,userida,oriname,comment)VALUES(?,?,?,?)";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkid());
		this.pstmt.setString(2, homework.getUserida());
		this.pstmt.setString(3, homework.getOriname());
		this.pstmt.setString(4, homework.getComment());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		this.pstmt.close();
		return flag;
	}
	
	public List<Homework> findUpload(String keyWord) throws SQLException{
		List<Homework> all=new ArrayList<Homework>();
		String sql="SELECT oriname,comment,homeworkid,userida FROM upload_homework WHERE homeworkid like ?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, "%"+keyWord+"%");
		ResultSet rs=this.pstmt.executeQuery();
		Homework homework=null;
		while(rs.next()){
			homework=new Homework();
			homework.setOriname(rs.getString(1));
			homework.setComment(rs.getString(2));
			homework.setHomeworkid(rs.getString(3));
			homework.setUserida(rs.getString(4));
			all.add(homework);
		}
		return all;
	}
	public boolean checkUpload(Homework homework) throws SQLException{
		boolean flag=false;
		String sql="SELECT * FROM upload_homework WHERE homeworkid=? AND userida=?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkid());
		this.pstmt.setString(2, homework.getUserida());
		ResultSet rs=this.pstmt.executeQuery();
		if(rs.next()){
			homework.setOriname(rs.getString(1));
			homework.setComment(rs.getString(2));
			flag=true;
		}
		if(this.pstmt!=null){
			this.pstmt.close();
		}
		return flag;
	}
	public boolean doScore(Homework homework)throws SQLException{
		boolean flag=false;
		String sql="INSERT INTO score_homework(homeworkid,userida,useridc,score)VALUES(?,?,?,?)";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkid());
		this.pstmt.setString(2, homework.getUserida());
		this.pstmt.setString(3, homework.getUserid());
		this.pstmt.setString(4, homework.getScore());
		if(this.pstmt.executeUpdate()>0){
			flag=true;
		}
		this.pstmt.close();
		return flag;
	}
	public List<Homework> findScore(String keyWord1,String keyWord2) throws SQLException{
		List<Homework> all=new ArrayList<Homework>();
		String sql="SELECT homeworkid,useridc,score FROM score_homework WHERE homeworkid=? AND userida=?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1,keyWord1);
		this.pstmt.setString(2,keyWord2);
		ResultSet rs=this.pstmt.executeQuery();
		Homework homework=null;
		while(rs.next()){
			homework=new Homework();
			homework.setHomeworkid(rs.getString(1));
			homework.setUserid(rs.getString(2));
			homework.setScore(rs.getString(3));
			all.add(homework);
		}
		return all;
	}
	public boolean findAvg(Homework homework)throws SQLException{
		boolean flag=false;
		String sql="SELECT AVG(score),homeworkid FROM score_homework WHERE homeworkid=? AND userida=?";
		this.pstmt=this.conn.prepareStatement(sql);
		this.pstmt.setString(1, homework.getHomeworkid());
		this.pstmt.setString(2, homework.getUserida());
		ResultSet rs=this.pstmt.executeQuery();
		if(rs.next()){
			homework.setAvgscore(rs.getString(1));
			homework.setHomeworkid(rs.getString(2));
			flag=true;
		}
		if(this.pstmt!=null){
			this.pstmt.close();
		}
		return flag;
	}
}
